/*
 * Copyright (c) 2016. For Intelligent Group.
 */

package com.intelligent.ispc.client.ex;

import javax.swing.FocusManager;
import javax.swing.*;
import java.awt.*;

/**
 * Created by Peter on 2016/7/8.
 */
public class JTextFieldEx extends JTextField {

    private String placeHolder = "";

    /**
     * Constructor
     */
    public JTextFieldEx() {
        super();
    }


    /**
     * Constructor
     *
     * @param text init text.
     */
    public JTextFieldEx(String text) {
        super(text);
    }

    /**
     * Constructor
     *
     * @param columns the max columns size.
     */
    public JTextFieldEx(int columns) {
        super(columns);
    }

    /**
     * Constructor
     *
     * @param text    init text.
     * @param columns the max columns size.
     */
    public JTextFieldEx(String text, int columns) {
        super(text, columns);
    }

    @Override
    protected void paintComponent(java.awt.Graphics g) {
        super.paintComponent(g);
        final int fontSize = 12;
        final int x = 7;
        final int y = 15;

        if (getText().isEmpty() && !(FocusManager.getCurrentKeyboardFocusManager().getFocusOwner() == this)) {
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setBackground(Color.gray);
            g2.setFont(getFont().deriveFont(Font.CENTER_BASELINE, fontSize));
            this.setForeground(Color.GRAY);
            g2.drawString(placeHolder, x, y); //figure out x, y from font's FontMetrics and size of component.
            g2.dispose();
        } else {
            this.setForeground(Color.BLACK);
        }
    }

    public String getPlaceHolder() {
        return placeHolder;
    }

    public void setPlaceHolder(String placeHolder) {
        this.placeHolder = placeHolder;
    }
}
